package com.np.xxpermissions1.permission;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.np.xxpermissions1.R;

/**
 * 用于权限授权提示窗口
 */
public class DialogPermission extends DialogFragmentFullscreen {

    @SuppressLint("StaticFieldLeak")
    private static DialogPermission m_dialogFragment;
    private OnSelectListener m_listener;

    public enum Type {NextTime, Grant}

    public interface OnSelectListener {
        void onClick(Type type);
    }

    /**
     * 显示权限授权窗口
     */
    public static void show(FragmentActivity activity, OnSelectListener listener) {
        if (activity != null) {
            DialogPermission.hide();
            {
                m_dialogFragment = new DialogPermission();
                m_dialogFragment.m_gravity = Gravity.CENTER;
                m_dialogFragment.m_offset_x = 0;
                m_dialogFragment.m_offset_y = 0;
                m_dialogFragment.m_width = WindowManager.LayoutParams.WRAP_CONTENT;
                m_dialogFragment.m_height = WindowManager.LayoutParams.WRAP_CONTENT;
                m_dialogFragment.m_alpha = 1.0f;
                m_dialogFragment.m_dimAmount = 0.0f;
                m_dialogFragment.m_listener = listener;
            }
            m_dialogFragment.show(activity.getSupportFragmentManager(), "DialogPermission");
        }
    }

    /**
     * 隐藏权限授权窗口
     */
    public static void hide() {
        if (m_dialogFragment != null) {
            m_dialogFragment.dismiss();
            m_dialogFragment = null;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        super.requireDialog().setCancelable(false);
        super.requireDialog().setCanceledOnTouchOutside(false);
        super.requireDialog().getWindow().setBackgroundDrawableResource(R.drawable.helper_shape_window_round_back_permission);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_permission, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        // 外部存储权限已经授权则隐藏
        if (XXPermissions.isGranted(super.requireActivity(), Permission.MANAGE_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE)) {
            view.findViewById(R.id.permission_storage).setVisibility(View.GONE);
        }

        // 系统设置权限已经授权则隐藏
        if (XXPermissions.isGranted(super.requireActivity(), Permission.WRITE_SETTINGS)) {
            view.findViewById(R.id.permission_setting).setVisibility(View.GONE);
        }

        // 下次再说
        view.findViewById(R.id.button_next_time).setOnClickListener(v -> {
            this.dismiss();
            if (m_listener != null) m_listener.onClick(Type.NextTime);
        });

        // 去授权
        view.findViewById(R.id.button_grant).setOnClickListener(v -> {
            this.dismiss();
            if (m_listener != null) m_listener.onClick(Type.Grant);
        });
    }

    @Override
    public void onKeyBack() {
        super.onKeyBack();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}